package org.jeecg.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jdk.vm.ci.code.site.Site;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.system.entity.BizEquipmentEntity;
import org.jeecg.modules.system.entity.BizSiteEntity;
import org.jeecg.modules.system.entity.EqPowerEntity;
import org.jeecg.modules.system.entity.SitePowerEntity;
import org.jeecg.modules.system.mapper.BizEquipmentMapper;
import org.jeecg.modules.system.mapper.BizSiteMapper;
import org.jeecg.modules.system.mapper.JournalMapper;
import org.jeecg.modules.system.service.IJournalService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service("JournalServiceImpl")
public class JournalServiceImpl implements IJournalService {
    @Resource
    BizEquipmentMapper bizEquipmentMapper;

    @Resource
    ISysDictService sysDictService;

    @Resource
    JournalMapper journalMapper;


    @Override
    public List<Map<?, ?>> getReportData(String startDate, String endDate,String type,String siteId) {

        QueryWrapper<BizEquipmentEntity> queryEquipment = new QueryWrapper();
        queryEquipment.eq("status","1");
        if(StringUtils.isNotEmpty(siteId)){
            queryEquipment.eq("site_id",siteId);
        }
        queryEquipment.notIn("eq_type", Arrays.asList(1));


        List<BizEquipmentEntity> BizEquipmentEntity = bizEquipmentMapper.selectList(queryEquipment);

        List<Map<?, ?>> result = new ArrayList<>();
        for(BizEquipmentEntity e:BizEquipmentEntity){
            List meterData = null;

            if("day".equals(type)){
                meterData = journalMapper.getDayMeterReport(startDate,endDate,e.getEqId());
            }else if("month".equals(type)){
                meterData = journalMapper.getMonthMeterReport(startDate,endDate,e.getEqId());
            }else{
                meterData = journalMapper.getYearMeterReport(startDate,endDate,e.getEqId());
            }


            Map equipment = new HashMap();
            equipment.put("eqCode",e.getEqCode());
            equipment.put("eqName",e.getEqName());
            equipment.put("eqType",e.getEqType());
            equipment.put("eqTypeText",sysDictService.queryDictTextByKey("dict_eq_type",e.getEqType()));
            equipment.put("meterData",meterData);
            result.add(equipment);
        }

        return result;
    }

    @Override
    public List<Map<?, ?>> getEqReportData(String startDate, String endDate, String type, String eqId) {
        BizEquipmentEntity e =  bizEquipmentMapper.selectById(eqId);

        List<Map<?, ?>> result = new ArrayList<>();

        if(e == null) return result;

        List meterData = null;

        if("day".equals(type)){
            meterData = journalMapper.getDayMeterReport(startDate,endDate,eqId);
        }else if("month".equals(type)){
            meterData = journalMapper.getMonthMeterReport(startDate,endDate,eqId);
        }else if("year".equals(type)){
            meterData = journalMapper.getYearMeterReport(startDate,endDate,eqId);
        }else if("min".equals(type)){
            meterData = journalMapper.getMinMeterReport(startDate,endDate,eqId);
        }else{
            meterData = journalMapper.getHourMeterReport(startDate,endDate,eqId);
        }


        Map equipment = new HashMap();
        equipment.put("eqCode",e.getEqCode());
        equipment.put("eqName",e.getEqName());
        equipment.put("meterData",meterData);
        result.add(equipment);

        return result;
    }

    @Override
    public List<Map<?, ?>> getSiteReportData(String startDate, String endDate, String type, String siteId) {
        List<Map<?, ?>> result = new ArrayList<>();
        List siteData = null;
        List siteData3 = null;

        if(StringUtils.isEmpty(siteId)) siteId=null;

        startDate = startDate + " 00:00:00";
        endDate = endDate + " 23:59:59";

        if("hour".equals(type)){
            siteData = journalMapper.getHourSiteReport(startDate,endDate,siteId,"'2','5'");
            siteData3 = journalMapper.getHourSiteReport(startDate,endDate,siteId,"'3'");
        }else if("day".equals(type)){
            siteData = journalMapper.getDaySiteReport(startDate,endDate,siteId,"'2','5'");
            siteData3 = journalMapper.getDaySiteReport(startDate,endDate,siteId,"'3'");
        }else if("month".equals(type)){
            siteData = journalMapper.getMonthSiteReport(startDate,endDate,siteId,"'2','5'");
            siteData3 = journalMapper.getMonthSiteReport(startDate,endDate,siteId,"'3'");
        }else if("min".equals(type)){
            siteData = journalMapper.getMinSiteReport(startDate,endDate,siteId,"'2','5'");
            siteData3 = journalMapper.getMinSiteReport(startDate,endDate,siteId,"'3'");
        }else{
            siteData = journalMapper.getYearSiteReport(startDate,endDate,siteId,"'2','5'");
            siteData3 = journalMapper.getYearSiteReport(startDate,endDate,siteId,"'3'");
        }


        Map equipment = new HashMap();
        equipment.put("siteId",siteId);
        equipment.put("meterData",siteData);
        equipment.put("meterData3",siteData3);
        result.add(equipment);

        return result;
    }

    @Override
    public List<Map<?, ?>> getEqTypeCount(String siteId) {
        return journalMapper.getEqTypeCount(siteId);
    }

    @Override
    public List<EqPowerEntity> getEqPowerTotal(String startDate, String endDate, String eqId) {
        startDate = startDate + " 00:00:00";
        endDate = endDate + " 23:59:59";
        return journalMapper.getEqPowerTotal(startDate,endDate,eqId);
    }

    public List<EqPowerEntity> getEqPowerTotalBySite(String startDate, String endDate, String siteId,String sortKey,String sortType) {
        startDate = startDate + " 00:00:00";
        endDate = endDate + " 23:59:59";
        return journalMapper.getEqPowerTotalBySite(startDate,endDate,siteId,sortKey,sortType);
    }

    @Override
    public List<Map<String,Object>> getSitePowerTotal(String startDate, String endDate, String siteId,String eqType) {
        startDate = startDate + " 00:00:00";
        endDate = endDate + " 23:59:59";
        return journalMapper.getSitePowerTotal(startDate,endDate,siteId,eqType);
    }

    @Override
    public List<Map<?, ?>> getAlarmReportData(String startDate, String endDate, String type,String eqId) {
        List<Map<?, ?>> result = new ArrayList<>();
        List alarmData = null;

        if(StringUtils.isEmpty(eqId)) eqId=null;

        if("hour".equals(type)){
            alarmData = journalMapper.getHourAlarmReport(startDate,endDate,eqId);
        }else if("day".equals(type)){
            alarmData = journalMapper.getDayAlarmReport(startDate,endDate,eqId);
        }else if("month".equals(type)){
            alarmData = journalMapper.getMonthAlarmReport(startDate,endDate,eqId);
        }else{
            alarmData = journalMapper.getYearAlarmReport(startDate,endDate,eqId);
        }


        Map alarm = new HashMap();
        alarm.put("alarmData",alarmData);
        result.add(alarm);

        return result;
    }
}
